<?php
/*
 * @Descripttion:
 * @Author: Twoknives
 * @Date: 2021-09-22 20:18:10
 * @LastEditTime: 2021-10-05 12:23:31
 * @FilePath: \njz\app\api\controller\Client.php
 */

namespace app\api\controller;

// use AlibabaCloud\Client\AlibabaCloud;
// use AlibabaCloud\Client\Exception\ClientException;
// use AlibabaCloud\Client\Exception\ServerException;
use AlibabaCloud\SDK\Dyplsapi\V20170525\Dyplsapi;
use AlibabaCloud\SDK\Dyplsapi\V20170525\Models\BindAxnExtensionRequest;
use AlibabaCloud\SDK\Dyplsapi\V20170525\Models\UnbindSubscriptionRequest;
use app\common\controller\ApiController;
use Darabonba\OpenApi\Models\Config;
use think\App;
use think\facade\Db;

use think\facade\Env;
class Client extends ApiController
{

    protected $noNeedLogin = ['index', 'Client'];
    protected $mustLogin = [];
    // protected $accessKeyId =  Env::get('aliyunClint.accessKeyId');
    // protected $accessKeySecret = Env::get('aliyunClint.accessKeySecret');
    // protected $PoolKey = Env::get('aliyunClint.PoolKey');

    // public function bindAxn()
    // {
    //     AlibabaCloud::accessKeyClient($this->accessKeyId, $this->accessKeySecret)->regionId('cn-hangzhou')->asDefaultClient();
    //     try {
    //         $param = ['PhoneNoA'=>15002908872,'PoolKey'=>$this->PoolKey,'Extension'=>22,'PhoneNoX'=>'17169000507','PhoneNoB'=>15002908872,'Expiration'=> date('Y-m-d H:i:s',strtotime("+10 minute"))];
    //         $result = AlibabaCloud::rpc()->product('Dyplsapi')->version('2017-05-25')->action('BindAxnExtension')->method('POST')->host('dyplsapi.aliyuncs.com')->options(['query' => $param])->request();
    //         print_r($result->toArray());
    //         if ($result->Code == 'OK' && $result->Message == 'OK') {
    //             $this->success('绑定成功',$result->toArray());
    //         } else {
    //             $this->error($result->Message);
    //         }
    //     } catch (ClientException $e) {
    //         $this->error($e->getErrorMessage());
    //         // echo $e->getErrorMessage() . PHP_EOL;
    //     } catch (ServerException $e) {
    //         $this->error($e->getErrorMessage());
    //         // echo $e->getErrorMessage() . PHP_EOL;
    //     }
    // }

    /**
     * 使用AK&SK初始化账号Client
     * @param string $accessKeyId
     * @param string $accessKeySecret
     * @return Dyplsapi Client
     */
    public static function createClient($accessKeyId, $accessKeySecret)
    {
        $config = new Config([
            // 您的AccessKey ID
            "accessKeyId" => $accessKeyId,
            // 您的AccessKey Secret
            "accessKeySecret" => $accessKeySecret,
        ]);
        // 访问的域名
        $config->endpoint = "dyplsapi.aliyuncs.com";
        return new Dyplsapi($config);
    }

    /**
     * 绑定电话
     *
     * @return void
     */
    public function bindAxn()
    {
        $param = $this->request->param();
        if (empty($param['phoneNoA'])) {
            $this->error('参数有误');
        }
        $phone = $param['phoneNoA'];
        $expiration = date('Y-m-d H:i:s', strtotime("+2 minute"));
        $exit = Db::name('anx')->where('phone', $phone)->where('expire_time', '>',time())->find();
        $client = self::createClient(Env::get('aliyunClint.accessKeyId'), Env::get('aliyunClint.accessKeySecret'));
        // if (!empty($exit)) {
        //     if ($exit['expire_time'] > strtotime('-5 minute', time())) {
        //         $this->success('绑定成功', ['extension' => $exit['extension'], 'secretNo' => $exit['secretNo']]);
        //     } else {
        //         // Db::name('anx')->where('phone', $phone)->delete();
        //         $unbindSubscriptionRequest = new UnbindSubscriptionRequest(["SubsId" => $exit['subsId'], "SecretNo" => $exit['secretNo']]);
        //         $dd = $client->unbindSubscription($unbindSubscriptionRequest);
        //         echo json_encode($dd);exit;
        //     }
        // } 
        if (!empty($exit)) {
            $this->success('绑定成功', ['extension' => $exit['extension'], 'secretNo' => $exit['secretNo']]);
        }
        $bindAxnExtensionRequest = new BindAxnExtensionRequest(["phoneNoA" => $phone, "expiration" => $expiration]);
        $result = $client->bindAxnExtension($bindAxnExtensionRequest);
        $body = $result->body;
        if ($body->code == 'OK' && $body->message == 'OK') {
            $secretBindDTO = $body->secretBindDTO;
            $rst = Db::name('anx')->insert(['phone' => $phone, 'extension' => $secretBindDTO->extension, 'expire_time' => strtotime($expiration), 'subsId' => $secretBindDTO->subsId, 'secretNo' => $secretBindDTO->secretNo]);
            if ($rst) {
                $this->success('绑定成功', ['extension' => $secretBindDTO->extension, 'secretNo' => $secretBindDTO->secretNo]);
            } else {
                $this->error('绑定失败');
            }
        } else {
            $this->error($body->message);
        }
    }

    // /**
    //  * @param string[] $args
    //  * @return void
    //  */
    // public static function main($args){
    //     $client = self::createClient("accessKeyId", "accessKeySecret");
    //     $bindAxnRequest = new BindAxnRequest([
    //         "poolKey" => "FC100000138726094"
    //     ]);
    //     // 复制代码运行请自行打印 API 的返回值
    //     $client->bindAxn($bindAxnRequest);
    // }

}
